package com.stock.dao;

import java.util.Date;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * Buysave entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.stock.dao.Buysave
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class BuysaveDAO {
	private static final Logger log = LoggerFactory.getLogger(BuysaveDAO.class);
	// property constants
	public static final String USER_NAME = "userName";
	public static final String NUMBER = "number";
	public static final String BUY_PRICE = "buyPrice";
	public static final String BUY_QTY = "buyQty";
	public static final String BUY_AMOUNT = "buyAmount";
	public static final String SALE_QTY = "saleQty";
	public static final String SALE_PRICE = "salePrice";
	public static final String SALE_AMOUNT = "saleAmount";
	public static final String OWN_QTY = "ownQty";
	public static final String OWN_AMOUNT = "ownAmount";
	public static final String BENIFIT = "benifit";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Buysave transientInstance) {
		log.debug("saving Buysave instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Buysave persistentInstance) {
		log.debug("deleting Buysave instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Buysave findById(java.lang.Integer id) {
		log.debug("getting Buysave instance with id: " + id);
		try {
			Buysave instance = (Buysave) getCurrentSession().get(
					"com.stock.dao.Buysave", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Buysave instance) {
		log.debug("finding Buysave instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("com.stock.dao.Buysave")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Buysave instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Buysave as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List findByNumber(Object number) {
		return findByProperty(NUMBER, number);
	}

	public List findByBuyPrice(Object buyPrice) {
		return findByProperty(BUY_PRICE, buyPrice);
	}

	public List findByBuyQty(Object buyQty) {
		return findByProperty(BUY_QTY, buyQty);
	}

	public List findByBuyAmount(Object buyAmount) {
		return findByProperty(BUY_AMOUNT, buyAmount);
	}

	public List findBySaleQty(Object saleQty) {
		return findByProperty(SALE_QTY, saleQty);
	}

	public List findBySalePrice(Object salePrice) {
		return findByProperty(SALE_PRICE, salePrice);
	}

	public List findBySaleAmount(Object saleAmount) {
		return findByProperty(SALE_AMOUNT, saleAmount);
	}

	public List findByOwnQty(Object ownQty) {
		return findByProperty(OWN_QTY, ownQty);
	}

	public List findByOwnAmount(Object ownAmount) {
		return findByProperty(OWN_AMOUNT, ownAmount);
	}

	public List findByBenifit(Object benifit) {
		return findByProperty(BENIFIT, benifit);
	}

	public List findAll() {
		log.debug("finding all Buysave instances");
		try {
			String queryString = "from Buysave";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Buysave merge(Buysave detachedInstance) {
		log.debug("merging Buysave instance");
		try {
			Buysave result = (Buysave) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Buysave instance) {
		log.debug("attaching dirty Buysave instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Buysave instance) {
		log.debug("attaching clean Buysave instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BuysaveDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BuysaveDAO) ctx.getBean("BuysaveDAO");
	}
}